/**
 * 
 */
package com.cqcis.uip.module.salary.oper.service.impl.ratio;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cqcis.uip.module.salary.oper.SalaryConstants;
import com.cqcis.uip.module.salary.oper.dao.ContractDetailDao;
import com.cqcis.uip.module.salary.oper.dao.jdbc.ContractJdbcDao;
import com.cqcis.uip.module.salary.oper.domain.ContractDetail;
import com.cqcis.uip.module.salary.oper.domain.Employe;
import com.cqcis.uip.module.salary.oper.domain.SalaryMain;
import com.cqcis.uip.module.salary.oper.domain.SalaryTCDetail;
import com.cqcis.uip.module.salary.oper.domain.extend.ContractRatio;
import com.cqcis.uip.module.salary.oper.service.RatioService;
import com.cqcis.uip.module.salary.oper.service.impl.ProcessServiceImpl;

/**
 * @author 李世军
 *
 * 创建于 2012-3-7 下午02:16:51
 */
public class RatioServiceImpl implements RatioService {
	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(ProcessServiceImpl.class);
	private ContractJdbcDao contractJdbcDao ;
	private ContractDetailDao contractDetailDao ;
	
	public void setContractDetailDao(ContractDetailDao contractDetailDao) {
		this.contractDetailDao = contractDetailDao;
	}

	public void setContractJdbcDao(ContractJdbcDao contractJdbcDao) {
		this.contractJdbcDao = contractJdbcDao;
	}

	
	
	/* (non-Javadoc)
	 * @see com.cqcis.uip.module.salary.oper.service.RatioFeeService#getRatioFee(com.cqcis.uip.module.salary.oper.domain.Employe)
	 */
	public void setSalary(Employe employe, SalaryMain main, List<SalaryTCDetail> details) {
		// 获取总业绩
		List<ContractRatio> contractRatios = contractJdbcDao.findContractRatio(employe.getId()) ;
		for(ContractRatio contractRatio : contractRatios){
			// 课程费
			List<ContractDetail> dtls = contractDetailDao.findContractDetail(contractRatio.getId()) ;
			for(ContractDetail dtl : dtls){
				SalaryTCDetail detail = new SalaryTCDetail() ;
				detail.setEmploye_id(employe.getId()) ;
				detail.setTrainingcontract_id(contractRatio.getId()) ;
				detail.setCourse_id(dtl.getCourse_id()) ;
				detail.setE1(dtl.getPay_prices()) ;
				detail.setE2(dtl.getPay_card_prices()) ;
				detail.setRebate(dtl.getRebate()) ;
				detail.setSelf_ratio(contractRatio.getRatio()) ;
				detail.setStudy_type(dtl.getStudy_type()) ;
				//extend
				detail.setTcType(contractRatio.getContract_type()) ;
				detail.setStudentId(contractRatio.getStudent_id()) ;
				detail.setRet_ratio(contractRatio.getRet_ratio()) ;
				details.add(detail) ;
			}
			
			// 交通费
			if(contractRatio.getTraffic_fee()>0 && (contractRatio.getTraffic_fee_pay()+contractRatio.getTraffic_fee_card())>0){
				SalaryTCDetail detailJT = new SalaryTCDetail() ;
				detailJT.setEmploye_id(employe.getId()) ;
				detailJT.setTrainingcontract_id(contractRatio.getId()) ;
				detailJT.setCourse_id(SalaryConstants.COURSE_JT) ;
				detailJT.setE1(contractRatio.getTraffic_fee_pay()) ;
				detailJT.setE2(contractRatio.getTraffic_fee_card()) ;
				//log.info("pay:"+contractRatio.getTraffic_fee_pay()+"card:"+contractRatio.getTraffic_fee_card()+"-"+contractRatio.getTraffic_fee()) ;
				detailJT.setRebate((contractRatio.getTraffic_fee_pay()+contractRatio.getTraffic_fee_card())/contractRatio.getTraffic_fee()) ;
				detailJT.setSelf_ratio(contractRatio.getRatio()) ;
				//extend
				detailJT.setTcType(contractRatio.getContract_type()) ;
				detailJT.setStudentId(contractRatio.getStudent_id()) ;
				detailJT.setRet_ratio(contractRatio.getRet_ratio()) ;
				details.add(detailJT) ;
			}
			
			// 综合服务费
			if(contractRatio.getService_fee()>0 && (contractRatio.getService_fee_pay()+contractRatio.getService_fee_card())>0){
				SalaryTCDetail detailZH = new SalaryTCDetail() ;
				detailZH.setEmploye_id(employe.getId()) ;
				detailZH.setTrainingcontract_id(contractRatio.getId()) ;
				detailZH.setCourse_id(SalaryConstants.COURSE_ZH) ;
				detailZH.setE1(contractRatio.getService_fee_pay()) ;
				detailZH.setE2(contractRatio.getService_fee_card()) ;
				detailZH.setRebate((contractRatio.getService_fee_pay()+contractRatio.getService_fee_card())/contractRatio.getService_fee()) ;
				detailZH.setSelf_ratio(contractRatio.getRatio()) ;
				//extend
				detailZH.setTcType(contractRatio.getContract_type()) ;
				detailZH.setStudentId(contractRatio.getStudent_id()) ;
				detailZH.setRet_ratio(contractRatio.getRet_ratio()) ;
				details.add(detailZH) ;
			}
		}
		
		float e = 0L ;
		for(SalaryTCDetail dtl : details){
			e += ( dtl.getE1()+(dtl.getE2()*SalaryConstants.CONTRACT_SERVICE_CARD) ) * (dtl.getSelf_ratio()/100) ;
		}
		
		// 获取提成比例
		//log.info("[E]" + e) ;
		float ratio = SalaryConstants.getRelaRatio(e) ;
		
		// 设置单笔提成
		for(SalaryTCDetail dtl : details){
			if(SalaryConstants.COURSE_ZH.equals(dtl.getCourse_id())){
				if(SalaryConstants.CONTRACT_TYPE_NEW.equals(dtl.getTcType()) || SalaryConstants.CONTRACT_TYPE_CVT.equals(dtl.getTcType())){
					dtl.setReal_e((( dtl.getE1()+(dtl.getE2()*SalaryConstants.CONTRACT_SERVICE_CARD) ) * (dtl.getSelf_ratio()/100))) ;
					//dtl.setFee(dtl.getReal_e()*ratio) ;
					dtl.setFee(0F) ;
					// 综合服务费 取消业绩提成，只有200按比例提成
					//dtl.setRebate_fee(dtl.getReal_e()*ratio+SalaryConstants.CONTRACT_SERVICE_RATIO*dtl.getSelf_ratio()/100) ;
					dtl.setRebate_fee(SalaryConstants.CONTRACT_SERVICE_RATIO*dtl.getSelf_ratio()/100) ;
					continue ;
				}
			}else if(SalaryConstants.COURSE_JT.equals(dtl.getCourse_id())){
				dtl.setFee(0L) ;
				continue ;
			}else{
				if(SalaryConstants.CONTRACT_TYPE_RET.equals(dtl.getTcType())){// 退费
					float retRatio = contractJdbcDao.getRetRatio(dtl.getStudentId(),employe.getId()) ;
					if(retRatio==0){
						retRatio = ratio ;
						dtl.setRemark("当前学生:"+dtl.getStudentId()+"  无历史记录,退费比例按当前提成比例:"+ratio) ;
					}
					//退费责任罚款  (退费总额*0.04*罚款比例)
					float retFee =  ( dtl.getE1()+(dtl.getE2()*SalaryConstants.CONTRACT_SERVICE_CARD) ) * 0.04F * dtl.getRet_ratio()/100F ;
					
					e = 0L ;
					e += ( dtl.getE1()+(dtl.getE2()*SalaryConstants.CONTRACT_SERVICE_CARD) ) * (dtl.getSelf_ratio()/100) ;
					dtl.setFee(e*retRatio) ;
					dtl.setRebate_fee(e*retRatio*dtl.getRebate() + retFee) ;// 退费 + 退费罚款
					dtl.setRemark(dtl.getRemark()+"|退费:"+e*retRatio*dtl.getRebate() +"退费罚款:"+retFee) ;
					
				}else{
					dtl.setReal_e((dtl.getE1()+(dtl.getE2()*SalaryConstants.CONTRACT_SERVICE_CARD))*(dtl.getSelf_ratio()/100)) ;
					dtl.setFee(dtl.getReal_e()*ratio) ;
					dtl.setRebate_fee(dtl.getFee()*dtl.getRebate()) ;
				}
			}
			
		}
		
		
		// 总提成
		float tcFee = 0L ;
		for(SalaryTCDetail dtl : details){
			tcFee += dtl.getRebate_fee() ;
		}
		
		main.setTc_fee(tcFee) ;
		main.setTc_all_fee(0L) ;
		main.setTc_ratio(ratio) ;
		
	}

}
